<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2018/7/11
 * Time: 17:33
 */

namespace app\admin\controller;


use think\Db;

class ArticleController extends CommonController
{
    /**
     * 文章列表展示
     * @return string
     */
    public function articleList(){
        $rows = $_POST['rows'];
        $page = $_POST['page'];
        $where = $this->getDataByCampusid($_POST);
        $res = Db::table('ew_article')->limit($rows * ($page - 1), $rows)->where($where)->select();
        $result = Db::table('ew_articlecate')->select();
        foreach($res as $key=>&$val){
            for($i=0;$i<count($result);$i++){
                if($val['a_cate_id'] == $result[$i]['a_cate_id']){
                    $val['a_cate_id'] = $result[$i]['a_cate_name'];
                }
            }
        }
        $total = Db::table('ew_article')->count();
        $data['total'] = $total;
        $data['rows'] = $res;
        return json_encode($data);
    }
    
    /**
     * 文章添加页面
     * @return mixed
     */
    public function addArticle(){
        $res = Db::table('ew_articlecate')->select();
        $this->assign([
            'res' => $res
        ]);
        return $this->fetch('article/addarticle');
    }
    
    /**
     * 文章添加入库
     * @return string
     */
    public function insertArticle(){
        if($_FILES['fileImg']['name']==''){
            $returnData['status'] = 0;
            $returnData['msg'] = "图片不能为空";
            return json_encode($returnData);
        }
        if(trim($_POST['a_title'])==''){
            $returnData['status'] = 0;
            $returnData['msg'] = "标题不能为空";
            return json_encode($returnData);
        }
        if(trim($_POST['a_content'])==''){
            $returnData['status'] = 0;
            $returnData['msg'] = "内容不能为空";
            return json_encode($returnData);
        }
//        if(trim($_POST['a_report'])==''){
//            $returnData['status'] = 0;
//            $returnData['msg'] = "参考报告不能为空";
//            return json_encode($returnData);
//        }
        if(trim($_POST['a_num'])==''){
            $returnData['status'] = 0;
            $returnData['msg'] = "阅读量不能为空";
            return json_encode($returnData);
        }
        if(trim($_POST['a_author'])==''){
            $returnData['status'] = 0;
            $returnData['msg'] = "作者不能为空";
            return json_encode($returnData);
        }
        $rand = date('YmdHis').rand(100000,999999);
        $exe = explode('.',$_FILES['fileImg']['name']);
        $filePath = $_FILES['fileImg']['tmp_name'];
        $img_name = $rand.'.'.$exe[1];
        $_filePath = "static/image/article/".$img_name;
        move_uploaded_file($filePath,$_filePath);
        $data['a_image'] = $_filePath;
        $data['a_title'] = $_POST['a_title'];
        $data['a_content'] = $_POST['a_content'];
        $data['a_report'] = $_POST['a_report'];
        $data['a_num'] = $_POST['a_num'];
        $data['a_author'] = $_POST['a_author'];
        $data['a_cate_id'] = $_POST['a_cate_id'];
        $data['a_createtime'] = date('Y-m-d');
        $res = Db::table('ew_article')->insert($data);
        if($res){
            return json_encode(array("status" => 1, "msg" => "添加成功！"));
        }else{
            return json_encode(array("status" => 0, "msg" => "添加失败！"));
        }
    }
    
    /**
     * 文章修改页面
     * @return mixed
     */
    public function updateArticle(){
        $a_id = $_POST['a_id'];
        $res = Db::table('ew_article')->where('a_id',$a_id)->find();
        $result = Db::table('ew_articlecate')->select();
        $this->assign([
            'res' => $res,
            'result' => $result
        ]);
        return $this->fetch('article/updatearticle');
    }
    
    /**
     * 文章修改入库
     * @return string
     */
    public function updateA(){
        if(!empty($_FILES['fileImg']['type'])){
            $rand = date('YmdHis').rand(100000,999999);
            $exe = explode('.',$_FILES['fileImg']['name']);
            $filePath = $_FILES['fileImg']['tmp_name'];
            $img_name = $rand.'.'.$exe[1];
            $_filePath = 'static/image/article/'.$img_name;
            move_uploaded_file($filePath,$_filePath);
            $data['a_image'] = $_filePath;
        }
        if(trim($_POST['a_title'])==''){
            $returnData['status'] = 0;
            $returnData['msg'] = "标题不能为空";
            return json_encode($returnData);
        }
        if(trim($_POST['a_content'])==''){
            $returnData['status'] = 0;
            $returnData['msg'] = "内容不能为空";
            return json_encode($returnData);
        }
//        if(trim($_POST['a_report'])==''){
//            $returnData['status'] = 0;
//            $returnData['msg'] = "参考报告不能为空";
//            return json_encode($returnData);
//        }
        if(trim($_POST['a_num'])==''){
            $returnData['status'] = 0;
            $returnData['msg'] = "阅读量不能为空";
            return json_encode($returnData);
        }
        if(trim($_POST['a_author'])==''){
            $returnData['status'] = 0;
            $returnData['msg'] = "作者不能为空";
            return json_encode($returnData);
        }
        $data['a_title'] = $_POST['a_title'];
        $data['a_content'] = $_POST['a_content'];
        $data['a_report'] = $_POST['a_report'];
        $data['a_num'] = $_POST['a_num'];
        $data['a_author'] = $_POST['a_author'];
        $data['a_cate_id'] = $_POST['a_cate_id'];
        $data['a_createtime'] = date('Y-m-d');
        $res = Db::table('ew_article')->where('a_id',$_POST['a_id'])->update($data);
        if($res){
            $returnData['status'] = 1;
            $returnData['msg'] = "修改成功";
            return json_encode($returnData);
        }else{
            $returnData['status'] = 0;
            $returnData['msg'] = "修改失败";
            return json_encode($returnData);
        }
    }
    
    /***
     * 文章删除
     * @return string
     *
     */
    public function deleteArticle(){
        $a_id = $_POST['a_id'];
        if ($a_id != '') {
            $where['a_id'] = $a_id;
            $res = Db::table('ew_article')->where($where)->delete();
            if ($res) {
                $returnData['status'] = 1;
                $returnData['msg'] = "删除成功";
                return json_encode($returnData);
            } else {
                $returnData['status'] = 0;
                $returnData['msg'] = "删除失败";
                return json_encode($returnData);
            }
        }
    }
    
    /**
     * 分类展示
     * @return string
     */
    public function articleCate(){
        $rows = $_POST['rows'];
        $page = $_POST['page'];
        $res = Db::table('ew_articlecate')->limit($rows * ($page - 1), $rows)->select();
        $total = Db::table('ew_articlecate')->count();
        $data['total'] = $total;
        $data['rows'] = $res;
        return json_encode($data);
    }
    
    /**
     * 文章添加页面
     * @return mixed
     */
    public function addArticleCate(){
        return $this->fetch('article/addarticlecate');
    }
    
    /**
     * 文章添加入库
     * @return string
     */
    public function insertArticleCate(){
        if(trim($_POST['a_cate_name'])==''){
            $returnData['status'] = 0;
            $returnData['msg'] = "分类名称不能为空";
            return json_encode($returnData);
        }
        $data['a_cate_name'] = $_POST['a_cate_name'];
        $res = Db::table('ew_articlecate')->insert($data);
        if($res){
            return json_encode(array("status" => 1, "msg" => "添加成功！"));
        }else{
            return json_encode(array("status" => 0, "msg" => "添加失败！"));
        }
    }
    
    /**
     * 分类修改页面
     * @return mixed
     */
    public function updateArticleCate(){
        $a_cate_id = $_POST['a_cate_id'];
        $res = Db::table('ew_articlecate')->where('a_cate_id',$a_cate_id)->find();
        $this->assign('res',$res);
        return $this->fetch('article/updatearticlecate');
    }
    
    /**
     * 分类修改入库
     * @return string
     */
    public function updateC(){
        if(trim($_POST['a_cate_name'])==''){
            $returnData['status'] = 0;
            $returnData['msg'] = "分类名称不能为空";
            return json_encode($returnData);
        }
        $data['a_cate_name'] = $_POST['a_cate_name'];
        $res = Db::table('ew_articlecate')->where('a_cate_id',$_POST['a_cate_id'])->update($data);
        if($res){
            $returnData['status'] = 1;
            $returnData['msg'] = "修改成功";
            return json_encode($returnData);
        }else{
            $returnData['status'] = 0;
            $returnData['msg'] = "修改失败";
            return json_encode($returnData);
        }
    }
    
    /***
     * 分类删除
     * @return string
     *
     */
    public function deleteArticleCate(){
        $a_cate_id = $_POST['a_cate_id'];
        if ($a_cate_id != '') {
            $result = Db::table('ew_article')->where('a_cate_id', $a_cate_id)->find();
            if($result){
                $returnData['status'] = 0;
                $returnData['msg'] = "该分类下有文章，不允许删除，请先删除文章";
                return json_encode($returnData);
            }
            $where['a_cate_id'] = $a_cate_id;
            $res = Db::table('ew_articlecate')->where($where)->delete();
            if ($res) {
                $returnData['status'] = 1;
                $returnData['msg'] = "删除成功";
                return json_encode($returnData);
            } else {
                $returnData['status'] = 0;
                $returnData['msg'] = "删除失败";
                return json_encode($returnData);
            }
        }
    }
}